import CryptoJS from 'crypto-js'
import { searchText } from '../../lang/zh'
// import Moment from 'moment'
// 年月日
export function format(date) {
  let year = date.getFullYear()
  let month = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : '0'+(date.getMonth() + 1)
  let day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
  return `${year}-${month}-${day}`
}
// 年月日 时分
export function formatTime(date) {
  let year = date.getFullYear()
  let month = date.getMonth() + 1 > 9 ? date.getMonth() + 1 : '0'+(date.getMonth() + 1)
  let day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate()
  let hour = date.getHours()> 9 ? date.getHours() : '0'+date.getHours()
  let min = date.getMinutes() > 9 ? date.getMinutes() : '0'+date.getMinutes()
  return `${year}-${month}-${day} ${hour}:${min}`
}
// 加密密码
let aeskey = CryptoJS.enc.Utf8.parse('o7H8uIM2O5qv65l2')
export const $cryptoUtils = {
  // 加密
  getAesString: password => {
    let encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(password), aeskey, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    })
    return encrypted.toString()
  }
}

// 出口运输状态
export const exportState = [
    {
      value: 0,
      label: 'cExport.all'
    },
    {
      value: 1,
      label: 'cExport.allocated'
    },
    {
      value: 2,
      label: 'cExport.extracted'
    },
    {
      value: 3,
      label: 'cExport.uncustomed'
    },
    {
      value: 4,
      label: 'cExport.released'
    },
    {
      value: 5,
      label: 'cExport.arrival'
    },
    {
      value: 6,
      label: 'cExport.notSigned'
    },
    {
      value: 7,
      label: 'cExport.finished'
    },
    {
      value: 8,
      label: 'cExport.closed'
    }
  ]
// 进口运输状态
export const transportState = [
  {
    value: -2,
    label: 'state.all'
  },
  {
    value: 1,
    label: 'state.declared'
  },
  {
    value: 2,
    label: 'state.declaredBut'
  },
  {
    value: 3,
    label: 'state.releasedFor'
  },
  {
    value: 4,
    label: 'state.pickedUp'
  },
  {
    value: 5,
    label: 'state.warehousing'
  },
  {
    value: 6,
    label: 'state.completed'
  },
  {
    value: -1,
    label: 'state.receivedFor'
  },
  {
    value: 0,
    label: 'state.shippedBut'
  }
]

// 转换文件格式
export const toFormData = function(val) {
  let formData = new FormData()
      for (let i in val) {
        isArray(val[i], i)
      }

      function isArray(array, key) {
        if (array == undefined || typeof array == 'function') {
          return false
        }
        if (typeof array != 'object') {
          formData.append(key, array)
        } else if (array instanceof Array) {
          if (array.length == 0) {
            formData.append(`${key}`, '')
          } else {
            for (let i in array) {
              // 判断文件
              if (array[i] instanceof File) {
                formData.append(`${key}`, array[i])
              } else if (typeof array[i] != 'object') {
                formData.append(`${key}`, array[i])
              } else {
                for (let j in array[i]) {
                  isArray(array[i][j], `${key}[${i}].${j}`)
                }
              }
            }
          }
        }
        // else if (array instanceof Moment) {
        //   formData.append(`${key}`, array['_d'])
        // }
        else {
          let arr = Object.keys(array)
          if (arr.indexOf('uid') == -1) {
            for (let j in array) {
              isArray(array[j], `${key}.${j}`)
            }
          } else {
            formData.append(`${key}`, array)
          }
        }
      }
      return formData
}

// 解图片base64
export const transformArrayBufferToBase64 = function(buffer) {
  var binary = ''
  var bytes = new Uint8Array(buffer)
  for (var len = bytes.byteLength, i = 0; i < len; i++) {
    binary += String.fromCharCode(bytes[i])
  }
  return window.btoa(binary)
}

// 出口选项报关状态
export const declare = [
  {
    value: 0,
    label: 'cExport.all'
  },
  {
    value: 3,
    label: 'cExport.uncustomedState'
  },
  {
    value: 4,
    label: 'cExport.declarationState'
  },
  {
    value: 5,
    label: 'cExport.releasedState'
  }
]

// 专车运输状态码
export const transoprtState = [
  {
    id: 1,
    name: '待审批'
  },
  {
    id: 2,
    name: '已通过已分拨'
  },
  {
    id: 3,
    name: '已驳回'
  },
  {
    id: 4,
    name: '已通过未分拨'
  }
]

// 审批状态
export const approvalState = [
  [
    {
    id: -5,
    name: 'cArroval.allDomestic'
    },

    {
      id: 1,
      name: 'cArroval.waitDomestic'
    },
    {
      id: 3,
      name: 'cArroval.passNo'
    },
    {
      id: 4,
      name: 'cArroval.passedd'
    },
    {
      id: 5,
      name: 'cArroval.noDomestic'
    }
  ]
]

// 时间段
export const timer = ['8:00~12:00','12:00~17:00','17:00以后','不限时间']

// 运输查询状态
export const searchState = [
  {
    id: 2,
    text: 'cTransportSearch.notLoaded'
  },
  {
    id: 4,
    text: 'cTransportSearch.stowaged'
  },
  {
    id: 5,
    text: 'cTransportSearch.allocate'
  },
  {
    id: 6,
    text: 'cTransportSearch.departure'
  },
  {
    id: 7,
    text: 'cTransportSearch.arrived'
  },
  {
    id: 8,
    text: 'cTransportSearch.completed'
  },
  {
    id: 9,
    text: 'cTransportSearch.canceled'
  }
]

// 国内运输/陆运运输/运输管理状态 ['未配载','已配载','已发出','已到达','已签收','已完成','已取消']
export const managementState = [
  {
    id: -2,
    name: 'm.all'
  },
  {
    id: 2,
    name: 'cTransportSearch.notLoaded'
  },
  {
    id: 4,
    name: 'cTransportSearch.stowaged'
  },
  {
    id: 5,
    name: 'cTransportSearch.allocate'
  },
  {
    id: 6,
    name: 'cTransportSearch.departure'
  },
  {
    id: 7,
    name: 'cTransportSearch.arrived'
  },
  {
    id: 9,
    name: 'cTransportSearch.canceled'
  }
]

export function getToday() {
  let today = new Date()
  let year = today.getFullYear()
  let date = today.getMonth()+1
  let day = today.getDate()
  let sevenDay = new Date(today.getTime() - 7*24*60*60*1000)
  let weekYear = sevenDay.getFullYear()
  let weekDate = sevenDay.getMonth()+1
  let weekToDay = sevenDay.getDate()
  return [`${weekYear+'-'+weekDate+'-'+weekToDay}`, `${year+'-'+date+'-'+day}`]
}
// 随附信息
export const fileName = [
  { name: ' 调拨系统《航材申请单》',value: '1' },
  { name: ' ME系统《发料单》',value: '2'},
  { name: '《(单位名称)器材接/发通知单》',value: '3'},
  { name: '《修理合同》或《(单位名称)器材接/发通知单》',value: '4'},
  { name: '《工具设备地面运输出库单》',value: '5'},
  { name: ' 邮件：说明事由，有申请人署名',value: '6' }
]

// 陆运签收字段
export const landReceiptArr = [
  {
    name: 'cTransportSearch.actualSendcarsDate', // 派车时间
    key: 'actualSendcarsDate'
  },
  {
    name: 'cArroval.aog', // 紧急度
    key: 'aog',
    span: {
      nameSpan: 5,
      keySpan: 19
    },
    check: result => {
      return result ? '是' : '否'
    }
  }
  ,
  {
    name: 'cArroval.searchTransportType', // 运输类型
    key: 'transportType'
  },
  {
    name: 'cTransportSearch.documentNum', // 单证号
    key: 'documentNum',
    span: {
      nameSpan: 5,
      keySpan: 19
    }
  },
  {
    name: 'cTransportSearch.start', // 始发站
    key: 'startStation',
    span: {
      nameSpan: 5,
      keySpan: 19
    }
  },
  {
    name: 'cTransportSearch.end', // 终点站
    key: 'targetStation',
    span: {
      nameSpan: 5,
      keySpan: 19
    }
  },
  {
    name: 'cTransportSearch.deliveryUnit', // 交货单位
    key: 'deliveryUnit'
  },
  {
    name: 'cTransportSearch.deliveryPeople', // 交货人
    key: 'deliveryPeople',
    span: {
      nameSpan: 5,
      keySpan: 19
    }
  },
  {
    name: 'cTransportSearch.deliveryPhone', // 交货电话
    key: 'deliveryPhone',
    span: {
      nameSpan: 7,
      keySpan: 17
    }
  },
  {
    name: 'cTransportSearch.receiveUnit', // 收货单位
    key: 'receiveUnit'
  },
  {
    name: 'cTransportSearch.receivePeople', // 收货人
    key: 'receivePeople',
    span: {
      nameSpan: 5,
      keySpan: 19
    }
  },
  {
    name: 'cTransportSearch.receivePhone', // 收货人电话
    key: 'receivePhone',
    span: {
      nameSpan: 7,
      keySpan: 17
    }
  },
  {
    name: 'cTransportSearch.driverName', // 司机姓名
    key: 'driverName'
  },
  {
    name: 'cTransportSearch.driverPhone', // 司机电话
    key: 'driverPhone'
  },
  {
    name: 'cTransportSearch.carNum', // 车牌号
    key: 'carNum',
    span: {
      nameSpan: 5,
      keySpan: 19
    }
  }
]

// 陆运签收件号详情
export const partNumArr = [
  {
    name1: 'cArroval.aog', // 紧急度
    key1: 'aog',
    check: (result, str) => {
      if (str === 'aog') return result ? '是' : '否'
      if (str === 'addTime' && result) return result.slice(0, 11)
      return ''
    },
    name2: 'cTransportSearch.addTime', // 新增日期
    key2: 'addTime'
  },
  {
    name1: 'cTransportSearch.itemName', // 货物名称
    key1: 'itemName',
    name2: 'cTransportSearch.partNum', // 件号
    key2: 'partNum'
  },
  {
    name1: 'cTransportSearch.serialNum', // 序号
    key1: 'serialNum',
    name2: 'cExport.shippedQuantity', // 件数
    key2: 'number'
  },
  {
    name1: 'cTransportSearch.danger', // 危险品
    key1: 'danger',
    check: (result, str, obj) => {
      if (str === 'danger' && result) return result === -1 ? '否' : '是'
      if (str === 'size' && obj.itemLength) return `${obj.itemLength} * ${obj.itemWidth} * ${obj.itemHeight} ${obj.measurement}`
      return ''
    },
    name2: 'cTransportSearch.size', // 尺寸
    key2: 'size'
  },
  {
    name1: 'cTransportSearch.itemWeight', // 重量
    key1: 'itemWeight',
    check: (result, str, obj) => {
      if (str === 'itemWeight' && result) return result + obj.weightUofm
      if (str === 'packingType' && result) {
        return searchText[result.split('.')[1]]
      }
      return ''
    },
    name2: 'cTransportSearch.packingType', // 包装类型
    key2: 'packingType'
  }
]
